const joinBody = {
    data: [
        "taking a moment to clear my mind. Please come in, you are welcome.",
        "预训练音色",//"自然语言控制",
        "中文女",
        "",
        null,
        null,
        "",
        //"Selene 'Moonshade', is a mysterious, elegant dancer with a connection to the night. Her movements are both mesmerizing and deadly.",
        0,
        false,
        1
    ],
    event_data: null,
    fn_index: 1,
    session_hash: "26g9pw06ego",
    trigger_id: 20,
}
export async function downloadWav(text, path) {
    let body = JSON.parse(JSON.stringify(joinBody));
    body.data[0] = text;
    let resp = await fetch(`${Bun.env.COSYVOICE_DEMO_WEBUI_ENDPOINT}/queue/join`, { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' } });
    //console.log(await resp.text());
    resp = await fetch(`${Bun.env.COSYVOICE_DEMO_WEBUI_ENDPOINT}/queue/data?session_hash=26g9pw06ego`);
    const respText = await resp.text();
    const urlStartAt = respText.lastIndexOf(`${Bun.env.COSYVOICE_DEMO_WEBUI_ENDPOINT}/stream`);
    if (urlStartAt < 0) {
        console.log('error:', respText);
        return;
    }
    const urlEndAt = respText.indexOf('"', urlStartAt);
    const url = respText.slice(urlStartAt, urlEndAt);
    //console.log('wav @', url);
    resp = await fetch(url);
    await Bun.write(path, resp);
}